//
//  PrivateLetterController.m
//  collection
//
//  Created by 靖 张 on 12/14/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "PrivateLetterController.h"
#import "PrivateLetterContentController.h"
#import "CollectionDataManager.h"
#import "PrivateLetterItem.h"
#import "PrivateLetterCell.h"
#import "AppDelegate.h"

@interface PrivateLetterController()

@property (retain, nonatomic) IBOutlet UITableView *privateLetterTableView;
@property (nonatomic, strong)   NSArray*   threadList;

@end

@implementation PrivateLetterController

@synthesize privateLetterTableView = _privateLetterTableView;
@synthesize threadList = _threadList;

@synthesize homepageRootController = _homepageRootController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"私信";
    }
    return self;
}

- (void)dealloc {
    
    self.threadList = nil;
    [_privateLetterTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView* emptyView = [[UIView alloc] init];
    emptyView.backgroundColor = [UIColor clearColor];
    self.privateLetterTableView.tableFooterView = emptyView;
    [emptyView release];
}

- (void)viewDidUnload
{
    [self setPrivateLetterTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.threadList = nil;
}

// Reload the list before view is being showen
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self refresh];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return [self.threadList count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* privateLetterCellID = @"PrivateLetterCellID";
    
    PrivateLetterCell* cell = (PrivateLetterCell*)[tableView dequeueReusableCellWithIdentifier:privateLetterCellID];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"PrivateLetterItemCell" owner:self options:nil];
        cell = [views objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    PrivateLetterThread* oneThread = [self.threadList objectAtIndex:indexPath.row];
    
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    UserInfo* user = [sharedDataManager cacheUserByUserID:oneThread.objectID];
    
    // Avatar
    cell.avatarView.image = user.avatar;
    
    // Object username
    cell.nicknameLabel.text = user.nickname;
   
    // Recent private letter content
    PrivateLetterItem* recentLetter = [oneThread.letterList lastObject];
    cell.lastMessageLabel.text = recentLetter.text;
    
    return cell;
}

#pragma mark - Implementation of UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
    PrivateLetterContentController* letterContentController = [[PrivateLetterContentController alloc] initWithNibName:@"PrivateLetterContentController" bundle:nil];
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    NSDictionary* letterSet = sharedDataManager.privateLetterSet;
    long long userID = sharedDataManager.currentLoginUser.userID;
    NSArray* threads = [letterSet objectForKey:[NSString stringWithFormat:@"%lld", userID]];
    PrivateLetterThread* theThread = [threads objectAtIndex:indexPath.row];
    letterContentController.conversationObjectUID = theThread.objectID;
    NSLog(@"Object ID : %lld", theThread.objectID);
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:letterContentController animated:YES];
    [letterContentController release];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.0f;
}

#pragma mark - Public methods

- (void)refresh {
    
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    NSDictionary* letterSet = sharedDataManager.privateLetterSet;
    long long userID = sharedDataManager.currentLoginUser.userID;
    self.threadList = [letterSet objectForKey:[NSString stringWithFormat:@"%lld", userID]];
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:10];
    [tableView reloadData];
}

@end
